﻿using AbpVnext_Module.Dto;
using AbpVnext_Module.Iservice;
using AbpVnext_Module.Model;
using AbpVnext_Module.Repository;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;

namespace AbpVnext_Module.Service
{
    [Dependency(ServiceLifetime.Transient)]
    public class PermissionGrantsService : CrudAppService<
                                           PermissionGrants,
                                           PermissionGrantsDto, // 结果出参 
                                           PermissionGrantsPageResultDto,  //获取列表返回参数dto
                                           int, //主键
                                           PermissionGrantsPageDto, //查询入参
                                           PermissionGrantsCreateDto, //创建入参dto
                                           PermissionGrantsUpdateDto>  //更新入参dto
                                           , IPermissionGrantsService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="permissionGrantsRepository"></param>
        public PermissionGrantsService(IPermissionGrantsRepository permissionGrantsRepository) : base(permissionGrantsRepository)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="providerName"></param>
        /// <param name="providerKey"></param>
        /// <returns></returns>
        public async Task<bool> CreateGrantedAsync(string name, string providerName, string providerKey)
        {
            PermissionGrants permissionGrants = new PermissionGrants();
            permissionGrants.PermissionName = name;
            permissionGrants.ProviderKey = providerKey;
            permissionGrants.ProviderName = providerName;
            await Repository.InsertAsync(permissionGrants);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="names"></param>
        /// <param name="providerName"></param>
        /// <param name="providerKey"></param>
        /// <returns></returns>
        public async Task<bool> CreateMenyGrantedAsync(string[] names, string providerName, string providerKey)
        {
            foreach (string name in names)
            {
                await CreateGrantedAsync(name, providerName, providerKey);
            }
            return true;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [Dependency(ServiceLifetime.Transient)]
    [RemoteService(true)]
    public class PermissionGrantssService : IPermissionStore //, IRemoteService
    {

        public IPermissionGrantsRepository PermissionRepository { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="providerName"></param>
        /// <param name="providerKey"></param>
        /// <returns></returns>
        public async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey)
        {
            PermissionGrants permissionGrants = new PermissionGrants();
            permissionGrants.PermissionName = name;
            permissionGrants.ProviderKey = providerKey;
            permissionGrants.ProviderName = providerName;
            await PermissionRepository.InsertAsync(permissionGrants);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="names"></param>
        /// <param name="providerName"></param>
        /// <param name="providerKey"></param>
        /// <returns></returns>
        public async Task<MultiplePermissionGrantResult> IsGrantedAsync(string[] names, string providerName, string providerKey)
        {
            foreach (string name in names)
            {
                await IsGrantedAsync(name, providerName, providerKey);
            }
            return new MultiplePermissionGrantResult();
        }
    }
}
